package com.example.service.impl;

import com.example.domain.entity.MaterialPurchase;
import com.example.mapper.MaterialPurchaseMapper;
import com.example.service.IMaterialPurchaseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.example.domain.query.MaterialPurchaseQuery;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 物料采购表 服务实现类
 * </p>
 *
 * @author baomidou
 * @since 2025-05-22
 */
@Service
public class MaterialPurchaseServiceImpl extends ServiceImpl<MaterialPurchaseMapper, MaterialPurchase> implements IMaterialPurchaseService {

    @Override
    public IPage<MaterialPurchase> pageQuery(MaterialPurchaseQuery query, int pageNum, int pageSize) {
        Page<MaterialPurchase> page = new Page<>(pageNum, pageSize);
        QueryWrapper<MaterialPurchase> wrapper = new QueryWrapper<>();
        if (query != null) {
            if (query.getPurchaseId() != null) {
                wrapper.eq("purchase_id", query.getPurchaseId());
            }
            if (query.getMaterialName() != null) {
                wrapper.eq("material_name", query.getMaterialName());
            }
            if (query.getProviderId() != null) {
                wrapper.eq("provider_id", query.getProviderId());
            }
            if (query.getPurchaseDateStart() != null && query.getPurchaseDateEnd() != null) {
                wrapper.between("create_time", query.getPurchaseDateStart(), query.getPurchaseDateEnd());
            }
            if (query.getAuditStatus() != null) {
                wrapper.eq("audit_status", query.getAuditStatus());
            }
            if(query.getDeptId() != null) {
                wrapper.eq("dept_id", query.getDeptId());
            }
            if(query.getPersonId() != null) {
                wrapper.eq("person_id", query.getPersonId());
            }
        }
        return this.page(page, wrapper);
    }
}
